/*
    This file is part of Manhunt.

    Manhunt is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Manhunt is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Manhunt.  If not, see <https://www.gnu.org/licenses/>.
 */

package snw.manhunt;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.plugin.java.JavaPlugin;
import snw.manhunt.commands.*;

import java.util.logging.Level;

public final class Manhunt extends JavaPlugin {
    private static JavaPlugin instance;

    @Override
    public void onLoad() {
        saveDefaultConfig();
    }

    @Override
    public void onEnable() {
        getLogger().info("注册命令...");

        registerCommand("start", new StartCommand());
        registerCommand("forcestop", new ForceStopCommand());
        registerCommand("hunter", new HunterCommand());
        registerCommand("runner", new RunnerCommand());
        registerCommand("leave", new LeaveCommand());

        getLogger().info("注册事件处理器...");
        Bukkit.getPluginManager().registerEvents(new EventProcessor(), this);

        getLogger().info("完成!");

        instance = this; //暴露实例
    }

    @Override
    public void onDisable() {
        saveConfig();
    }

    public static JavaPlugin getInstance() {
        return instance;
    }

    private void registerCommand(String command, CommandExecutor commandExecutor) {
        if (Bukkit.getPluginCommand(command) != null) {
            //noinspection ConstantConditions IDEA 真是个大聪明，排除了 null 还在报警告
            Bukkit.getPluginCommand(command).setExecutor(commandExecutor);
        } else {
            getLogger().log(Level.SEVERE, "命令 '" + command + "' 未注册，可能是 plugin.yml 的问题，请联系作者寻求帮助。");
            Bukkit.getPluginManager().disablePlugin(this);
        }
    }
}
